#include <string>
#include <queue>
#include <mutex>
#include <atomic>
#include <memory>
#include <functional>
#include <thread>
#include <condition_variable>
#include "Connection.h"
using namespace std;

class ConnectionPool
{
public:
    static ConnectionPool *getConnectionPool();
    shared_ptr<Connection> getConnection();

private:
    ConnectionPool();

    bool loadConfig();

    void produceConnectionTask();

    void scanConnectionTask();
    
    // Mysql
    string _ip;
    unsigned short _port;
    string _username;
    string _password;
    string _dbname;
    // ConnectionPool
    int _initSize;
    int _maxSize;
    int _maxIdleTime; // 最大空闲时间
    int _connectionTimeout;

    queue<Connection *> _connQueue; // 存储mysql的连接队列
    mutex _queueMutex;              // 维护连接队列的互斥锁
    atomic_int _connCnt;            // 记录连接所创建的Connection的总数量
    condition_variable cv;          // 条件遍历，用于连接生产线程和连接消费线程的通信
};